from __future__ import division
from bsccommonlib.csv_persister import CsvPersister
from target_models import TargetObject, TargetSubmit, TargetGraphicData
import datetime
import utils

class TargetGraphicDataGenerator(object):
    def __init__(self, csv_file, target_id):
        self.target_data_list = []
        target_file_name = utils.get_target_file_name(csv_file, target_id)
        target_submit_list = CsvPersister(target_file_name).get_list(TargetSubmit)

        target_object = TargetObject()
        target_object.id = target_id
        target_object = CsvPersister(csv_file).get_item(target_object)
        if target_object is not None:
            submit_total_value = 0
            step_value = target_object.target_value / target_object.duration
            for day_index in range(target_object.duration):
                tgd = TargetGraphicData()
                tgd.date = str(datetime.date.fromordinal(target_object.start_date.toordinal() + day_index))
                tgd.target_value = (day_index + 1) * step_value
                if tgd.date.toordinal() > datetime.date.today().toordinal():
                    tgd.submit_value = None
                else:
                    for submit in target_submit_list:
                        if submit.issue_date.toordinal() == tgd.date.toordinal():
                            submit_total_value = submit.point + submit_total_value
                            break
                    tgd.submit_value = submit_total_value

                self.target_data_list.append(tgd)



    def get_data(self):
        return self.target_data_list




if __name__ == '__main__':
    generator = TargetGraphicDataGenerator('/tmp/test123.csv', 1373366654481)
    for data in generator.get_data():
        print '%s, %d, %s' % (data.date, data.target_value, data.submit_value)
